<template>
  <div>
    <AModal
      v-bind="$attrs"
      @close="modalEvent('close')"
      @ok="modalEvent('ok')"
      @open="modalEvent('open')"
      @cancel="modalEvent('cancel')"
    >
      <AForm ref="formRef" :model="form" auto-label-width>
        <AFormItem
          field="processName"
          label="工序名称"
          :rules="[{ required: true, message: '请输入工序名称' }]"
          :validate-trigger="['change', 'input', 'blur']"
        >
          <AInput v-model="form.processName"></AInput>
        </AFormItem>
        <AFormItem field="mainDirectorId" label="机长">
          <ASelect
            v-model="form.mainDirectorId"
            allow-search
            allow-clear
            placeholder="输入员工名称进行搜索"
          >
            <AOption
              v-for="(item, index) in employeeList"
              :key="index"
              :label="item.employeeName"
              :value="item.employeeId"
            ></AOption>
          </ASelect>
        </AFormItem>
        <AFormItem field="deputyDirectorIdList" label="辅助人员">
          <ASelect
            v-model="form.deputyDirectorIdList"
            allow-search
            allow-clear
            multiple
            placeholder="输入员工名称进行搜索"
          >
            <AOption
              v-for="(item, index) in employeeList"
              :key="index"
              :label="item.employeeName"
              :value="item.employeeId"
            ></AOption>
          </ASelect>
        </AFormItem>
      </AForm>
    </AModal>
  </div>
</template>

<script lang="ts" setup>
import { Message } from '@arco-design/web-vue'
import { EmployeeResItem, getEmployeeAllList } from '@renderer/api/employee'
import { editProcess, ProcessReq, ProcessResItem } from '@renderer/api/productionDep'
import { useMenuState } from '@renderer/store'
import { deleteNullObject } from '@renderer/utils/data-processing'

interface Props {
  [key: string]: any
  checkData?: Partial<ProcessResItem>
}
const props = withDefaults(defineProps<Props>(), {
  checkData: () => {
    return {}
  }
})
const emit = defineEmits(['update:visible', 'reload'])
const attrs = useAttrs()
const formRef = ref()
const form = ref<ProcessReq>({
  processName: '',
  mainDirectorId: '',
  deputyDirectorIdList: []
})
const menuParam = useMenuState()
const employeeList = ref<EmployeeResItem[]>([])

// 获取员工列表
const getEmployee = () => {
  getEmployeeAllList({}).then((res) => {
    employeeList.value = res.data.list
  })
}

// 弹窗事件
const modalEvent = (type: string) => {
  switch (type) {
    case 'ok':
      formRef.value.validate((errors: any) => {
        if (!errors) {
          // 新增
          if ((attrs.title as string).includes('新增')) {
            form.value.processId = undefined
          }
          editProcess(deleteNullObject(form.value)).then(() => {
            Message.success((attrs.title as string).includes('新增') ? '新增成功' : '修改成功')
            modalEvent('cancel')
            emit('reload')
          })
        }
      })
      break
    case 'close':
      menuParam.switchEditStatus(false)
      formRef.value.resetFields()
      emit('update:visible', false)
      break
    case 'open':
      getEmployee()
      Object.assign(form.value, props.checkData)
      menuParam.switchEditStatus(true)
      break
    case 'cancel':
      formRef.value.resetFields()
      emit('update:visible', false)
      menuParam.switchEditStatus(false)
      break
    default:
      break
  }
}
</script>
<style scoped>
.card-container {
  margin-bottom: 10px;
}
</style>
